package vn.easyforshopping;


import java.util.Arrays;

import vn.easyforshopping.pulltorefresh.PullToRefreshBase;
import vn.easyforshopping.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import vn.easyforshopping.pulltorefresh.PullToRefreshGridView;
import vn.easyforshopping.ui.SlidingMenu;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private PullToRefreshGridView mPullRefreshGridView;
	private GridView mGridView;
	private ArrayAdapter<String> mAdapter;
	
	SlidingMenu menuLeft;
	
	SlidingMenu menuRight;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// remove title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_main);
		
		menuLeft = new SlidingMenu(this);
        menuLeft.setMode(SlidingMenu.LEFT);
        menuLeft.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menuLeft.setBehindOffset(50);
        menuLeft.setFadeDegree(0.35f);
        menuLeft.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menuLeft.setMenu(R.layout.menu);
        
        menuRight = new SlidingMenu(this);
        menuRight.setMode(SlidingMenu.RIGHT);
        menuRight.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menuRight.setBehindOffset(50);
        menuRight.setFadeDegree(0.35f);
        menuRight.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menuRight.setMenu(R.layout.menu);
		
		// Lay tab host ra va init cac tab.
		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup();
		tabHost.setOnTabChangedListener(onTabChanged);
		
		mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
		mGridView = mPullRefreshGridView.getRefreshableView();
		// Set a listener to be invoked when the list should be refreshed.
				mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

					@Override
					public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
//						Toast.makeText(PullToRefreshGridActivity.this, "Pull Down!", Toast.LENGTH_SHORT).show();
//						new GetDataTask().execute();
					}

					@Override
					public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
//						Toast.makeText(PullToRefreshGridActivity.this, "Pull Up!", Toast.LENGTH_SHORT).show();
//						new GetDataTask().execute();
					}

				});
		mGridView.setAdapter(new ImageAdapter(this));
		
//		tabHost.scrollTo(30, 30);

//		final GridView gridview = (GridView) findViewById(R.id.gridview);
//		gridview.setAdapter(new ImageAdapter(this));

//		gridview.setOnItemClickListener(new OnItemClickListener() {
//			public void onItemClick(AdapterView<?> parent, View v,
//					int position, long id) {
//				Toast.makeText(MainActivity.this, "" + position,
//						Toast.LENGTH_SHORT).show();
//			}
//		});

		TabHost.TabSpec spec; // Resusable TabSpec for each tab - See more at:
								// http://blog.lardev.com/2011/02/02/how-to-move-android-t
		// abs-from-top-to-bottom/#sthash.NEsqQuZ2.dpuf
		Intent intent; // Reusable Intent for each tab

		View searchTabIndicator = createTabIndicator("Search", getResources()
				.getDrawable(R.drawable.ic_launcher), tabHost);

		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, Search.class);
		Resources res = getResources();
		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("search").setIndicator(searchTabIndicator)
				.setContent(new TabHost.TabContentFactory() {
					@Override
					public View createTabContent(String tag) {
//						return gridview;
						return mGridView;
					}
				});
		tabHost.addTab(spec);

		// Points tabs
		intent = new Intent();// .setClass(this, Points.class);
		spec = tabHost
				.newTabSpec("points")
				.setIndicator("Points", res.getDrawable(R.drawable.ic_launcher))
				.setContent(intent);
		tabHost.addTab(spec);

		// Social tabs
		// intent = new Intent().setClass(this, Social.class);
		spec = tabHost
				.newTabSpec("social")
				.setIndicator("Social", res.getDrawable(R.drawable.ic_launcher))
				.setContent(intent);
		tabHost.addTab(spec);

		// Contact tabs
		// intent = new Intent().setClass(this, Contact.class);
		spec = tabHost
				.newTabSpec("contact")
				.setIndicator("Contact",
						res.getDrawable(R.drawable.ic_launcher))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);

//		Log.d("Main Activity", gridview.getParent().toString());
	}

	/**
	 * bat su kien on tab changed.
	 */
	OnTabChangeListener onTabChanged = new OnTabChangeListener() {

		@Override
		public void onTabChanged(String tabId) {
			Log.d("ON TAB CHANGED", tabId);
		}
	};

	public void onClickButtonTitleLeft(View view) {
		Log.d("MAIN ACTYVITY", "click button title left");
		menuLeft.showMenu();
	}

	public void onClickButtonTitleRight(View view) {
		Log.d("MAIN ACTYVITY", "click button title right");
		menuRight.showMenu();
	}

	/**
	 * Khoi tao 1 tab indicator.
	 * 
	 * @param text
	 * @param image
	 * @param tabHost
	 * @return
	 */
	public View createTabIndicator(String text, Drawable image, TabHost tabHost) {
		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_widget, tabHost.getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(text);

		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageDrawable(image);
		icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
		return tabIndicator;
	}

	/**
	 * Set title lai cho 1 tab.
	 * 
	 * @param titleLabel
	 */
	public void setTabTitle(String titleLabel) {
		Log.d("SET TAB TITLE", titleLabel);
		TextView title = (TextView) findViewById(R.id.title_lable);
		title.setText(titleLabel);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
//	private class GetDataTask extends AsyncTask<Void, Void, String[]> {
//
//		@Override
//		protected String[] doInBackground(Void... params) {
//			// Simulates a background job.
//			try {
//				Thread.sleep(2000);
//			} catch (InterruptedException e) {
//			}
//			return mStrings;
//		}
//
//		@Override
//		protected void onPostExecute(String[] result) {
//			mListItems.addFirst("Added after refresh...");
//			mListItems.addAll(Arrays.asList(result));
//			mAdapter.notifyDataSetChanged();
//
//			// Call onRefreshComplete when the list has been refreshed.
//			mPullRefreshGridView.onRefreshComplete();
//
//			super.onPostExecute(result);
//		}
//	}

}
